const htmlwebpackplugin = require('html-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
const ex = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ASSETS_BUILD_PATH = path.resolve('./dist');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')


function generateHtml(filename, template) {
    let config = {
        filename: `view/${filename}.html`,
        template: `src/view/${template}.html`,
        inject: true,
        hash: true,
        chunks: ['common', filename]
    };
    return config;
}

module.exports = {
    entry: {
        'index': ['./src/page/index/index.js', './src/page/aside/index.js'],
        'common': ['./src/page/common/index.js'],
        'login': ['./src/page/login/index.js'],
        'trash': ['./src/page/trash/index.js'],
        'trash-certificate-photos': ['./src/page/trash-certificate-photos/index.js'],
        'file-location': ['./src/page/file-location/index.js'],
        '404': './src/page/404/index.js',
        '500': './src/page/500/index.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'js/[name].bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']

                    }
                }
            },
            {
                test: /\.css$/,
                loader: ex.extract({
                    fallback: "style-loader",
                    use: ['css-loader', 'postcss-loader']
                })
            },
            {
                test: /\.art$/,
                loader: 'html-loader',
            },
            {
                test: /\.(gif|png|jpg|jpeg|woff|svg|eot|ttf)\??.*$/,
                loader: 'url-loader?limit=100&name=/resource/[name].[ext]'
            }
        ]
    },
    resolve: {
        alias: {
            node_modules: path.resolve(__dirname, '/node_modules/'),
            'load-image': 'blueimp-load-image/js/load-image.js',
            'load-image-meta': 'blueimp-load-image/js/load-image-meta.js',
            'load-image-exif': 'blueimp-load-image/js/load-image-exif.js',
            'load-image-scale': 'blueimp-load-image/js/load-image-scale.js',
            'canvas-to-blob': 'blueimp-canvas-to-blob/js/canvas-to-blob.js',
            'jquery-ui/ui/widget': 'blueimp-file-upload/js/vendor/jquery.ui.widget.js',
            page: __dirname + '/src/page/',
            images: __dirname + '/src/images/',
            util: __dirname + '/src/util/',
            service: __dirname + '/src/service/',

        }
    },
    externals: {
        jquery: 'jQuery',
    },
    devtool: 'source-map',
    plugins: [
        new webpack.LoaderOptionsPlugin({ minimize: true }),
        new CleanWebpackPlugin([ASSETS_BUILD_PATH], { verbose: false }),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common',
            filename: 'js/common.js'
        }),
        new ex({
            filename: 'css/[name].css',
        }),
        new htmlwebpackplugin(generateHtml('index', 'index')),
        new htmlwebpackplugin(generateHtml('login', 'login')),
        new htmlwebpackplugin(generateHtml('trash', 'trash')),
        new htmlwebpackplugin(generateHtml('file-location', 'file-location')),
        new htmlwebpackplugin(generateHtml('trash-certificate-photos', 'trash-certificate-photos')),
        new htmlwebpackplugin(generateHtml('404', '404')),
        new htmlwebpackplugin(generateHtml('500', '500')),
    ]
}